<script setup lang="ts">
import { onMounted, ref } from 'vue'
import TreeDisplay from '@/components/TreeDisplay.vue'
import TagsDisplay from '@/components/TagsDisplay.vue'
import {
  contentData,
  loadContentIndex,
  fetchContent,
} from '@/services/contentService'
import type { ContentItem } from '@/models'

const displayMode = ref<'tree' | 'tags'>('tree')
const currentContent = ref<string>('')
const showDialog = ref<boolean>(false)

const handleContentClick = async (node: ContentItem) => {
  console.log('content-click:', node)
  const content = await fetchContent(node.path)
  currentContent.value = content
}

const openDialog = () => {
  showDialog.value = true
}

const closeDialog = () => {
  showDialog.value = false
}

onMounted(() => {
  loadContentIndex()
})

const toggleDisplayMode = () => {
  displayMode.value = displayMode.value === 'tree' ? 'tags' : 'tree'
}
</script>

<template>
  <div class="content-container">
    <div class="mode-toggle">
      <button @click="toggleDisplayMode">
        {{ displayMode === 'tree' ? '切换到标签视图' : '切换到树形视图' }}
      </button>
    </div>

    <div class="content-wrapper">
      <div class="content-display">
        <TreeDisplay
          v-if="displayMode === 'tree'"
          :data="contentData"
          @content-click="handleContentClick"
        />
        <TagsDisplay v-else :data="contentData" @item-click="handleContentClick" />
      </div>
      <div v-if="currentContent" class="content-preview">
        <button class="open-dialog-btn" @click="openDialog">
          全屏查看内容
        </button>
        <div v-html="currentContent"></div>
      </div>
    </div>

    <!-- 全屏弹窗 -->
    <div v-if="showDialog" class="fullscreen-dialog" @click="closeDialog">
      <div class="dialog-content" @click.stop>
        <div class="dialog-header">
          <h3>内容预览</h3>
          <button class="close-btn" @click="closeDialog">×</button>
        </div>
        <div class="dialog-body" v-html="currentContent"></div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.content-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.mode-toggle {
  margin-bottom: 20px;
  text-align: right;
}

.mode-toggle button {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.mode-toggle button:hover {
  background-color: #33a06f;
}

.content-wrapper {
  display: flex;
  gap: 20px;
}

.content-display {
  flex: 1;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 20px;
  min-height: 500px;
}

.content-preview {
  flex: 1;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 20px;
  min-height: 500px;
  overflow: auto;
}

.open-dialog-btn {
  padding: 8px 16px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 16px;
}

.open-dialog-btn:hover {
  background-color: #40a9ff;
}

.fullscreen-dialog {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  width: 90vw;
  height: 90vh;
  background-color: white;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #eee;
  background-color: #fafafa;
}

.dialog-header h3 {
  margin: 0;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #666;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #333;
  background-color: #f0f0f0;
  border-radius: 4px;
}

.dialog-body {
  flex: 1;
  padding: 24px;
  overflow: auto;
}
</style>
